package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringGroupActivitiesDao;
import com.catering.entity.CateringGroupActivities;
import com.catering.entity.vo.CateringGroupActivitiesVo;
import com.catering.service.CateringGroupActivitiesService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CateringGroupActivitiesServiceImpl extends ServiceImpl<CateringGroupActivitiesDao, CateringGroupActivities> implements CateringGroupActivitiesService {
	
	@Autowired
	private CateringGroupActivitiesDao cateringGroupActivitiesDao;

	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper<CateringGroupActivities> wrapper = CommonUtils.getKeySet(map,"t1");
			List<CateringGroupActivitiesVo> list = new ArrayList<CateringGroupActivitiesVo>();
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringGroupActivities> page = new Page<>(Integer.parseInt(map.get("page")),Integer.parseInt(map.get("limit")));
				IPage<CateringGroupActivitiesVo> pageList = cateringGroupActivitiesDao.selectPageList(page, wrapper);
				Integer count = cateringGroupActivitiesDao.selectPageCount(wrapper);
				list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				list = cateringGroupActivitiesDao.selectNoPageList(wrapper);
				return ResultData.success(list);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringGroupActivities vo = JSON.parseObject(JSON.toJSONString(map), CateringGroupActivities.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringGroupActivitiesDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringGroupActivities vo = JSON.parseObject(JSON.toJSONString(map), CateringGroupActivities.class);
			cateringGroupActivitiesDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,String> map) {
		try {
			CateringGroupActivities vo = JSON.parseObject(JSON.toJSONString(map), CateringGroupActivities.class);
			List<String> list = Stream.of(vo.getSysId().split(",")).collect(Collectors.toList());
			cateringGroupActivitiesDao.deleteBatchIds(list);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
